package com.szcinda.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface ZadOrderRepository extends JpaRepository<ZadOrder, String>, JpaSpecificationExecutor<ZadOrder> {
    ZadOrder findById(String id);

    List<ZadOrder> findByIdIn(List<String> ids);

    List<ZadOrder> findByAlertIsTrueAndOrganizationIdIn(List<String> orgIds);

    List<ZadOrder> findByOrderNumberIn(List<String> orderNumberList);

    List<ZadOrder> findByOrganizationNameIn(List<String> orgNames);

    long countByBusinessCode(String businessCode);

    ZadOrder findByOrderNumber(String orderNumber);

    @Query("select z from ZadOrder z where (z.invoice = ?1 or z.invoice is null) and z.organizationId in (?2)")
    List<ZadOrder> findInvoice(String invoice, List<String> orgIds);

    @Query("select z from ZadOrder z where (z.outInvoice = ?1 or z.outInvoice is null) and z.organizationId in (?2)")
    List<ZadOrder> findOutInvoice(String invoice, List<String> orgIds);

    @Query("select z from ZadOrder z where (z.settlement = ?1 or z.settlement is null) and z.organizationId in (?2)")
    List<ZadOrder> findSettlement(String settlement, List<String> orgIds);

    @Query("select z from ZadOrder z where (z.outSettlement = ?1 or z.outSettlement is null) and z.organizationId in (?2)")
    List<ZadOrder> findOutSettlement(String settlement, List<String> orgIds);

    List<ZadOrder> findByOrganizationId(String organizationId);
    List<ZadOrder> findByOrganizationIdIn(List<String> organizationIds);

    @Query("select z from ZadOrder z where z.organizationId = ?1 and z.clientId = ?2 and z.cabinetNumber = ?3 and z.bookNumber = ?4 and z.bookName = ?5 and z.businessCode = ?6 and z.originPlace = ?7 and z.destPlace = ?8")
    List<ZadOrder> findAllByOptions(String organizationId, String clientId, String cabinetNumber, String bookNumber, String bookName, String businessCode, String originPlace, String destPlace);
}
